
package com.datawin.taotaocheche.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.datawin.taotaocheche.R;
import com.datawin.taotaocheche.bean.RechargeSettingInfo;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * chenjian
 */
public class RechargeAdapter extends SuperBaseAdapter<RechargeSettingInfo> {
    Context mContext;
    DecimalFormat df = new DecimalFormat("0");
    List<RechargeSettingInfo> mData = new ArrayList<>();
    private int firstCurrIndex = 0;// 当前选中元素的下标

    public RechargeAdapter(Context context, List<RechargeSettingInfo> data) {
        super(context, data);
        mContext = context;
        mData = data;


    }

    @Override
    protected void convert(BaseViewHolder holder, RechargeSettingInfo item, final int position) {
        LinearLayout layout = holder.getView(R.id.rechagre_layout_one);
        TextView moeny = holder.getView(R.id.rechagre_one);
        TextView discount = holder.getView(R.id.rechagre_info_one);
        moeny.setText("充" + item.getRechargeAmount() + "元");
        if (item.getDiscount().equals("0")) {
            discount.setText("");
            discount.setVisibility(View.GONE);
        } else {
            discount.setText("首充送" + item.getDiscount() + "元");
            discount.setVisibility(View.VISIBLE);
        }
        if (item.isSelected()) {
            moeny.setTextColor(Color.parseColor("#FFFFFFFF"));
            discount.setTextColor(Color.parseColor("#FFFFFFFF"));
            layout.setBackgroundResource(R.drawable.bg_shape_blue);
        } else {
            moeny.setTextColor(Color.parseColor("#FF4A4A4A"));
            discount.setTextColor(Color.parseColor("#FFF85233"));
            layout.setBackgroundResource(R.drawable.bg_shape_gray);
        }

        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mData.get(position).isSelected()) {
                    mData.get(position).setSelected(false);
                } else {
                    if (firstCurrIndex >= 0) {
                        mData.get(firstCurrIndex)
                                .setSelected(false);
                    }
                    mData.get(position).setSelected(true);
                    // 记录当前选中的
                    firstCurrIndex = position;
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    protected int getItemViewLayoutId(int position, RechargeSettingInfo item) {
        return R.layout.item_recharge_info;
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

    }


}